package com.example.shop_2212.preorder

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.ParamOrder
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.shop_2212.R
import com.example.shop_2212.adapter.PreOrderAdapter
import com.example.shop_2212.databinding.ActivityPreOrderBinding

@Route(path = "/app/PreOrderActivity")
class PreOrderActivity : BaseActivity<ActivityPreOrderBinding,PreOrderViewModel>() {
    @Autowired
    @JvmField
    var preOrderNo = ""

    var addressId = 0//获取地址
    var proTotalFee = ""//总金额

    lateinit var preOrderAdapter: PreOrderAdapter
    override fun initView() {
        ARouter.getInstance().inject(this)

        preOrderAdapter = PreOrderAdapter()
        binding.rv.adapter = preOrderAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)
        viewModel.getPreOrder(preOrderNo)

        //点击创建订单
        binding.create.setOnClickListener {
            val paramOrder = ParamOrder()
            paramOrder.addressId =addressId //地址编号
            paramOrder.payChannel = "weixinh5" //支付渠道
            paramOrder.payType = "yue" //支付方式
            paramOrder.preOrderNo = preOrderNo //预下单编号
            paramOrder.shippingType = 1  //购物模式  配送
            viewModel.order(paramOrder)
        }
    }

    override fun initData() {
        viewModel.getSuccess.observe(this){
            if(it.code==Const.RESULT_OK){
                addressId = it.data.orderInfoVo.addressId
                proTotalFee = it.data.orderInfoVo.proTotalFee
                binding.tvName.text = "${it.data.orderInfoVo.realName}   ${it.data.orderInfoVo.phone}"
                binding.tvAddress.text = "${it.data.orderInfoVo.province}${it.data.orderInfoVo.city}${it.data.orderInfoVo.district}${it.data.orderInfoVo.detail}"
                binding.tvMoney.text = "合计:${it.data.orderInfoVo.proTotalFee}元"

                preOrderAdapter.data.clear()
                preOrderAdapter.data.addAll(it.data.orderInfoVo.orderDetailList)
                preOrderAdapter.notifyDataSetChanged()
            }
        }
        viewModel.orderSuccess.observe(this){
            if(it.code==Const.RESULT_OK){
                finish()
                ARouter.getInstance().build("/app/PayActivity")
                    .withString("orderNo",it.data.orderNo)
                    .withString("money",proTotalFee).navigation()
            }
        }
    }

    override fun getViewModel(): Class<PreOrderViewModel>  = PreOrderViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_pre_order

}